//
//  AXFScancodeController.m
//  BEEQUICK
//
//  Created by HH on 16/11/24.
//  Copyright © 2016年 SevenGroup. All rights reserved.
//

#import "AXFScancodeController.h"
#import <AVFoundation/AVFoundation.h>
#import "AXFDetailViewController.h"

#define qrcode_scanline_qrcodeHeight 170
@interface AXFScancodeController ()<AVCaptureMetadataOutputObjectsDelegate>

@property (nonatomic, strong) CADisplayLink *link;

@property (nonatomic, strong)  AVCaptureSession *session;

// 记录位置
@property (assign, nonatomic) CGFloat qrcode_scanline_qrcodeTop;


// 记录扫码视图
@property (weak, nonatomic)UIImageView *qrcode_scanline_qrcodeView;


//记录容器视图
@property (weak, nonatomic)UIView *containerView;

@end

@implementation AXFScancodeController

- (void)viewDidLoad {
    [super viewDidLoad];
}


// 设置界面
- (void)setupUI
{
    // 218  170
    // 添加视图
    UIView *containerView = [[UIView alloc]init];
//    containerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:containerView];
    containerView.clipsToBounds = YES;
    
    UIImageView *qrcode_borderView = [[UIImageView alloc]init];
    qrcode_borderView.image = [UIImage imageNamed:@"qrcode_border"];
    [containerView addSubview:qrcode_borderView];
    
    UIImageView *qrcode_scanline_qrcodeView  = [[UIImageView alloc]init];
    qrcode_scanline_qrcodeView.image = [UIImage imageNamed:@"qrcode_scanline_qrcode"];
    ;
    [containerView addSubview:qrcode_scanline_qrcodeView];
    
    
    // 设置布局
[containerView mas_makeConstraints:^(MASConstraintMaker *make) {
   
    make.center.equalTo(self.view);
    make.height.mas_equalTo(qrcode_scanline_qrcodeHeight);
    make.width.mas_equalTo(218);
}];
    
    [qrcode_borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(containerView);
    }];
    
    [qrcode_scanline_qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(containerView);
        make.top.equalTo(containerView).offset(-qrcode_scanline_qrcodeHeight);
    }];
    
    self.qrcode_scanline_qrcodeTop = -qrcode_scanline_qrcodeHeight;
    self.qrcode_scanline_qrcodeView = qrcode_scanline_qrcodeView;
    self.containerView = containerView;
    
    [self setupSaoyisao];
}


- (CADisplayLink *)link
{
    if (_link == nil) {
        //添加定时器
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeImageView)];
    }
    return _link;
}

//在view将要显示的时候,添加定时器
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    // 开始扫描
    [self.session startRunning];
    
    // 添加定时器
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
}

// 改变视图的位置
- (void)changeImageView
{
    _qrcode_scanline_qrcodeTop += 1;
    
    if (_qrcode_scanline_qrcodeTop > qrcode_scanline_qrcodeHeight) {
        _qrcode_scanline_qrcodeTop = -qrcode_scanline_qrcodeHeight;
    }
    
    [self.qrcode_scanline_qrcodeView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.containerView).offset(_qrcode_scanline_qrcodeTop);
    }];
    
}

//在view消失的时候 删除定时器
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.link invalidate];
    self.link = nil;
    
}


//设置二维码扫描内容
- (void)setupSaoyisao
{
    //1.创建输入对象
    //1.1获取摄像头
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc]initWithDevice:device error:nil];
    //2.创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc]init];
    //3.创建会话 连接输入和输出
    
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    
    if ([session canAddInput:input]) {
        
        [session  addInput:input];
    }
    if ([session canAddOutput:output]) {
        [session addOutput:output];
    }
    
    //4.设置输出的代理及输出元数据类型
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //5.创建显示图层
    
    
    AVCaptureVideoPreviewLayer *layer = [AVCaptureVideoPreviewLayer  layerWithSession:session];
    
    layer.frame = self.view.bounds;
    [self.view.layer insertSublayer:layer atIndex:0];
    
    //6.开始扫描
//    [session startRunning];
    
    self.session = session;
}



- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    AVMetadataMachineReadableCodeObject *obj = metadataObjects.lastObject;
    
    NSLog(@"%@", obj.stringValue);
    
    [self.session stopRunning];
    
    AXFDetailViewController *detailViewController = [[AXFDetailViewController alloc] init];
    
    detailViewController.urlStr = obj.stringValue;
    
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}



@end
